package com.qbkj.base_lib.ui;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.PaintDrawable;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.PopupWindow;
import android.widget.Toast;

import com.qbkj.base_lib.utils.CommonUtils;
import com.qbkj.base_lib.utils.ViewController;
import com.qbkj.base_lib.utils.TagUtils;
import com.zhy.autolayout.utils.AutoUtils;

/**
 * Created by St on 2018/4/19.
 */

public abstract class BasePopupWindow extends PopupWindow {
    private View contentView;
    protected ViewController q;
    protected Context mContext;
    protected Intent mIntent;
    protected Bundle mBundle;
    //屏幕缩放比例
    protected float pixi_width = 1;
    protected float pixi_height = 1;
    private Activity activity;

    public BasePopupWindow(int layoutId, Context context, View showview) {
        this.mContext = context;
        activity = (Activity) context;
        this.mIntent = new Intent();
        this.mBundle = new Bundle();
        if (layoutId != 0)
            contentView = LayoutInflater.from(context).inflate(layoutId, null, false);
        else contentView = LayoutInflater.from(context).inflate(onCreateLayout(), null, false);
        AutoUtils.autoSize(contentView);
        setContentView(contentView);
        q = new ViewController(context, contentView);
        setAnimationStyle(-1);
        pixi_width = CommonUtils.getScreenPixi(context)[0];
        pixi_height = CommonUtils.getScreenPixi(context)[1];
        //设置可以获取焦点
        setFocusable(true);
        //设置可以触摸弹出框以外的区域
        setOutsideTouchable(true);
        setBackgroundDrawable(new PaintDrawable(0x00000000));
        init(contentView);

    }

    public BasePopupWindow(Context context, View showview) {
        this(0, context, showview);

    }

    protected abstract int onCreateLayout();

    protected abstract void init(View contentView);

    protected void skipActivity(Class cls, int ResultCode) {
        skipActivity(cls, ResultCode, mIntent, mBundle);
    }

    protected void skipActivity(Class cls) {
        skipActivity(cls, TagUtils.NORESULTCODE, mIntent, mBundle);
    }

    protected void skipActivityFinish(Class cls) {
        skipActivity(cls);
        activity.finish();
    }

    protected void skipActivityFinish(Class cls, int ResultCode) {
        skipActivity(cls, ResultCode);
        activity.finish();
    }

    //跳转
    public void skipActivity(Class cls, int ResultCode, Intent intent, Bundle bundle) {
        intent.setClass(activity, cls);
        if (bundle != null) intent.putExtras(bundle);
        if (TagUtils.NORESULTCODE != ResultCode) {
            activity.startActivityForResult(intent, ResultCode);
        } else {
            activity.startActivity(intent);
        }
    }

    protected void toast(String text) {
        if (text != null) {
            Toast.makeText(mContext, text, Toast.LENGTH_SHORT).show();
        }
    }

}
